/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manage_image;

/**
 *
 * @author Admin
 */
import Manager_Image_Utils.ButtonCloseFullScreen;
import Manager_Image_Utils.Constants;
import Manager_Image_Utils.DraggableComponent;
import Manager_Image_Utils.DraggableImageComponent;
import Manager_Image_Utils.Library;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics; 
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment; 
import java.awt.Image; 
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
 
import javax.swing.JFrame; 
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
 
public class FullScreenPressKey extends JFrame { 
 
    // this line is needed to avoid serialization warnings  
    private static final long serialVersionUID = 1L;
    private int index=0;
    private float zoom=100;
    private float zoomold=100;
    ArrayList<File>ListFile=new ArrayList<File>();
    private File currentF;
    private File CurrentDirectory;
    private Color Bg=Color.black;
    private ButtonCloseFullScreen btnClose;
    private Constants c=new Constants();
    public static Image screenImage; // downloaded image  
    int w, h; // Display height and width 
    double x,y;
    int oldindex=0;
    boolean flag=true;
    boolean isrepaint=false;
    Thread threpain;
    Thread main;
    ArrayList <Thread> ArrayThread=new ArrayList<Thread>();
    
    
    Point PMouseA=new Point();
    Point PMouseB=new Point();
    Point PMouseC=new Point();
    public Boolean isdrag=false;
    public Boolean isdraged=false;
    JLabel imgview=new JLabel();
    // Program entry 
    public static void main(String[] args) throws Exception { 
        //new FullScreenPressKey(new File("E://img//"));
    } 
 
    // Class constructor  
    FullScreenPressKey(ArrayList<File> listfile) throws MalformedURLException, IOException { 
 
        // Exiting program on window close 
        imgview.setBorder(null);
       
        ListFile=listfile;
        if(ListFile.size()>0){
            currentF=ListFile.get(index);
        }
        addWindowListener(new WindowAdapter() { 
            public void windowClosing(WindowEvent e) { 
                System.exit(1);
            } 
        });
        addMouseWheelListener(new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                int notches = e.getWheelRotation();
                if (notches < 0) {
                    flag=false;
                    ZomOut();
                    repaint();
                } else {
                    flag=false;
                    ZomIn();
                    repaint();
                }
                
            }
        });
        // Exitig program on mouse click 
        addMouseListener(new MouseListener() { 
            public void mouseClicked(MouseEvent e) { 
                PMouseA.setLocation(e.getPoint());
                
            } 
            public void mousePressed(MouseEvent e) {
                PMouseA.setLocation(e.getPoint());
            } 
            public void mouseReleased(MouseEvent e) {
            } 
            public void mouseEntered(MouseEvent e) {
                setCursor(Cursor.DEFAULT_CURSOR);
                isdrag=false;
                isdraged=false;
            } 
            public void mouseExited(MouseEvent e) {
                setCursor(Cursor.DEFAULT_CURSOR);
            } 
        } 
        );
        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                setCursor(Cursor.MOVE_CURSOR);
                if(e.getPoint()!=null){
                    PMouseB.setLocation(e.getPoint());
                    isdrag=true;
                    repaint();
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                PMouseB.setLocation(e.getPoint());
            }
        });
        
        addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                {
                    for(int i=0;i<ArrayThread.size();i++)
                    {
                        ArrayThread.get(i).interrupt();
                    }
                    dispose();
                }
                else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
                    Next();
                }
                else if(e.getKeyCode()==KeyEvent.VK_LEFT){
                    Previous();
                }
                else if(e.getKeyCode()==KeyEvent.VK_DOWN){
                    ZomIn();
                }
                else if(e.getKeyCode()==KeyEvent.VK_UP){
                    ZomOut();
                }
                
                repaint();
            }

            @Override
            public void keyReleased(KeyEvent e) {
                
            }
        });
        // set style
        setBackground(Bg);
        
        // remove window frame  
        this.setUndecorated(true);
        this.setAlwaysOnTop(true);
        // window should be visible 
        this.setVisible(true);
        
        // switching to fullscreen mode 
        GraphicsEnvironment.getLocalGraphicsEnvironment().
        getDefaultScreenDevice().setFullScreenWindow(this);
 
        // getting display resolution: width and height 
        w = this.getWidth();
        h = this.getHeight();
        // loading image  
        
        screenImage=ImageIO.read(currentF);
    } 
 
    public void paint (Graphics g) { 
        g.setColor(Bg);
        g.fillRect(0, 0, w, h);
        try {
            screenImage=ImageIO.read(ListFile.get(index));
        } catch (IOException ex) {
            Logger.getLogger(FullScreenPressKey.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        int w = getWidth();  
        int h = getHeight();  
        int imageWidth = screenImage.getWidth(null); 
        int imageHeight = screenImage.getHeight(null);  

        double x =w>imageWidth? (w - (zoom/100) * imageWidth)/2:(imageWidth - (zoom/100) * w)/2;  
        double y =w>imageHeight? (h - (zoom/100) * imageHeight)/2:(imageHeight - (zoom/100) * h)/2; 
         
        Graphics2D g2 = (Graphics2D)getGraphics();
        g2.clearRect(getX(), getY(), w, h);
        if(isdrag)
        {
            ///System.out.print(PMouseC.getX());
            //System.out.print(PMouseC.getX());
           // x=PMouseC.getX();
           //y=PMouseC.getY();
        }
        if(isdrag)
        {
            x=x+(PMouseB.getX()-PMouseA.getX());
            y=y+(PMouseB.getY()-PMouseA.getY());
            PMouseC.setLocation(x, y);
            System.out.print(PMouseC.getX());
            System.out.print(PMouseC.getX());
            System.out.print("\n end");
            //isdrag=false;
        }
        AffineTransform at = AffineTransform.getTranslateInstance(x,y);
        PMouseC.setLocation(x, y);
        at.scale(zoom/100, zoom/100);  
        
        g2.drawImage(screenImage, at, null);
        
    } 
    protected void Next(){
        index=index+1;
        if(index>ListFile.size()-1)
            index=0;
    }
    protected void Previous(){
        index=index-1;
        if(index<0)
            index=ListFile.size()-1;
    }
    protected void ZomIn(){
        zoom=zoom-1;
        if(zoom<10)
            zoom=10;
    }
    protected void ZomOut(){
        zoom=zoom+1;
    }
    protected void DrawThumbnail(){
        int _x=0;
        int _y=getHeight()-10;
        for(File f :ListFile){
            
        }
    }
    public static int getRandom(int range) {
        int r = (int) (Math.random() * range) - range;
        return r;
    }
} 